#include <event.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "ksocket.h"

void read_callback(KSOCK_fd fd, short events, void *arg)
{
/*
	struct KSOCK_readbuffer *buf = (struct KSOCK_readbuffer *)arg;
	KSOCK_readbuf_readsocket(buf, fd);
	KSOCK_readbuf_length(buf);
	KSOCK_readbuf_data(buf);
	KSOCK_readbuf_read(buf, buf2, 5);

	KSOCK_writebuffer *wbuf;
	KSOCK_writebuf_write(buf, buf2, 5);
	KSOCK_writebuf_length(buf);
*/
}

void listen_callback(KSOCK_fd listenfd, short events, void *arg)
{
	KSOCK_fd fd;
	struct event *ev = malloc(sizeof(struct event));

	fd = KSOCK_svr_accept(listenfd);
	printf("fd %d conn...\n", fd);

	event_set(ev, fd, EV_READ|EV_PERSIST, read_callback, ev);
	event_add(ev, NULL);
}

void add_listen_event()
{
    struct event *ev;
	struct KSOCK_svr_opt svr_opt;
	KSOCK_fd fd;

	svr_opt.addr = KSOCK_INADDR_ANY;
	svr_opt.port = 6001;
	svr_opt.backlog = 5;

	ev = malloc(sizeof(struct event));
	fd = KSOCK_svr_listen_tcp(&svr_opt);
	printf("lfd = %d\n", fd);
	event_set(ev, fd, EV_READ|EV_PERSIST, listen_callback, ev);
	event_add(ev, NULL);
}

int main()
{
    event_init();
    add_listen_event();
    event_dispatch();
    return 0;
}

